﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using ICSharpCode.SharpZipLib.WebReference;


namespace Hisun.View.DataExchange
{
    public partial class Test_business : System.Web.UI.Page
    {

        ICSharpCode.SharpZipLib.WebReference1.SynBusinessItf cx = new ICSharpCode.SharpZipLib.WebReference1.SynBusinessItf();
        string strCon1 = System.Configuration.ConfigurationManager.ConnectionStrings["hisunConnectionString"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ip"] != null && Session["cid"] != null && Session["aid"] != null)
                {
                    TextBox9.Text = Session["ip"].ToString();
                    TextBox10.Text = Session["aid"].ToString();
                    TextBox11.Text = Session["cid"].ToString();

                    string s = "http://" + Session["ip"] + "/uapws/service/nc.itf.hz.syn.SynBusinessItf";
                    cx.Url = s;
                }
                else
                {
                    TextBox9.Text = "172.16.1.41:8041";
                    TextBox10.Text = "001";
                    TextBox11.Text = "101";
                    string s = "http://" + TextBox9.Text + "/uapws/service/nc.itf.hz.syn.SynBusinessItf";
                    cx.Url = s;
                }
            }
            else
            {

                string s = "http://" + TextBox9.Text + "/uapws/service/nc.itf.hz.syn.SynBusinessItf";
                cx.Url = s;

            }
        }

        protected void Button_StockIn_Click(object sender, EventArgs e)
        {
            string begindate = TextBox1.Text;
            string enddate = TextBox2.Text;
            String s = cx.getNC_productStockIn(TextBox10.Text, TextBox11.Text, begindate, enddate);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(s);

            //doc.Save("C:/Documents and Settings/Administrator/桌面/stockin.xml");
            XmlNodeList topM = doc.DocumentElement.ChildNodes;
            DataTable dt = new DataTable();
            dt.Columns.Add("billbid");//主键
            dt.Columns.Add("公司号");//公司号
            dt.Columns.Add("帐套号");
            dt.Columns.Add("单据类型");//单据类型
            dt.Columns.Add("仓库");//仓库
            dt.Columns.Add("单据日期");//单据日期
            dt.Columns.Add("单据号");//单据号

            dt.Columns.Add("部门");//部门

            //dt.Columns.Add("子表主键");//子表主键
            dt.Columns.Add("存货编码");//存货编码
            dt.Columns.Add("存货名称");//存货名称
            dt.Columns.Add("规格");//规格
            dt.Columns.Add("辅计量数量");//辅计量数据
            dt.Columns.Add("数量");//数量
            //string billtype, warehouse, billcode, billdate, pk_corp, cdptid, ncaccount;
            foreach (XmlElement element in topM)
            {
                if (element.Name.ToLower() == "bill")
                {
                    XmlNodeList nodelist = element.ChildNodes;
                    if (nodelist.Count > 0)
                    {
                        DataRow dr = dt.NewRow();
                        foreach (XmlElement el in nodelist)
                        {
                            XmlNodeList list = el.ChildNodes;
                            switch (el.Name)
                            {
                                case "bill_head":
                                    foreach (XmlElement li in list)
                                    {
                                        switch (li.Name)
                                        {
                                            //case "billid":
                                            //    dr["主键"] = li.InnerText;
                                            //    break;
                                            case "billtype":
                                                dr["单据类型"] = li.InnerText;
                                                //billtype = li.InnerText;
                                                break;
                                            case "warehouse":
                                                dr["仓库"] = li.InnerText;
                                                //warehouse = li.InnerText;
                                                break;
                                            case "billcode":
                                                dr["单据号"] = li.InnerText;
                                                //billcode = li.InnerText;
                                                break;
                                            case "billdate":
                                                dr["单据日期"] = li.InnerText;
                                                //billdate = li.InnerText;
                                                break;
                                            case "cdptid":
                                                dr["部门"] = li.InnerText;
                                                //cdptid = li.InnerText;
                                                break;
                                            case "pk_corp":
                                                dr["公司号"] = li.InnerText;
                                                //pk_corp = li.InnerText;
                                                break;
                                            case "ncaccount":
                                                dr["帐套号"] = li.InnerText;
                                                //ncaccount = li.InnerText;
                                                break;
                                        }
                                    }
                                    break;
                                case "body":
                                    //XmlNodeList li1= list.ChildNodes;
                                    foreach (XmlElement li in list)
                                    {
                                        DataRow dr1 = dt.NewRow();
                                        dr1["单据类型"] = dr["单据类型"];
                                        dr1["仓库"] = dr["仓库"];
                                        dr1["单据号"] = dr["单据号"];
                                        dr1["单据日期"] = dr["单据日期"];
                                        dr1["部门"] = dr["部门"];
                                        dr1["公司号"] = dr["公司号"];
                                        dr1["帐套号"] = dr["帐套号"];

                                        switch (li.Name)
                                        {

                                            case "entry":
                                                XmlNodeList list1 = li.ChildNodes;
                                                //int i;
                                                //for (i = 0; i < list.Count-1; i++)
                                                //{
                                                foreach (XmlElement li1 in list1)
                                                {

                                                    switch (li1.Name)
                                                    {
                                                        case "billbid":
                                                            dr1["billbid"] = li1.InnerText;
                                                            break;
                                                        case "invcode":
                                                            dr1["存货编码"] = li1.InnerText;
                                                            break;
                                                        case "invname":
                                                            dr1["存货名称"] = li1.InnerText;
                                                            break;
                                                        case "invspec":
                                                            dr1["规格"] = li1.InnerText;
                                                            break;
                                                        case "assistnum":
                                                            dr1["辅计量数量"] = li1.InnerText;
                                                            break;
                                                        case "number":
                                                            dr1["数量"] = li1.InnerText;
                                                            break;
                                                    }
                                                }
                                                dt.Rows.Add(dr1);
                                                //}
                                                break;
                                        }
                                    }
                                    break;
                            }
                        }
                        //dt.Rows.Add(dr);
                    }
                }
            };
            GridView1.DataSource = dt;
            GridView1.DataBind();
            int n = dt.Rows.Count;
            int i = 0;
            try
            {
                SqlConnection sqlCon1;
                // string strCon1 = "Data Source=localhost;Initial Catalog=hisun;Integrated Security=True";
                sqlCon1 = new SqlConnection(strCon1);
                sqlCon1.Open();

                string sql1 = @"delete from TABC_NC_StockIn";
                SqlCommand cmd1 = new SqlCommand(sql1, sqlCon1);
                cmd1.ExecuteNonQuery();
                cmd1.Dispose();

                foreach (DataRow dr in dt.Rows)
                {
                    //SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

                    //string acpd = dr["单据日期"].ToString() + " " + "00:00:00";
                    //string sql1 = "select 会计期间ID from TABC_JS_Accountperiod where 成本开始日期<='"+acpd+"' and 成本结账日期>='"+acpd+"'";
                    //SqlDataAdapter dataAdapter = new SqlDataAdapter(sql1, sqlCon1);
                    //DataSet ds = new DataSet();
                    //dataAdapter.Fill(ds, "table0");
                    //DataTable dt1=ds.Tables[0];
                    //string acpd1 = dt1.Rows[0][0].ToString();


                    string sql = @"if exists (select * from TABC_NC_StockIn where billbid=@billbid)
                            update TABC_NC_StockIn set 帐套号=@ncaccount,公司号=@pk_corp, 单据类型=@billtype,仓库=@warehouse,单据日期=@billdate,单据号=@billcode,部门=@cdptid,存货编码=@invcode,存货名称=@invname,规格=@invspec,辅计量数量=@assistnum,数量=@number where billbid=@billbid
                            else insert into TABC_NC_StockIn values(@pk_corp,@ncaccount,@billtype,@warehouse,@billdate,@billcode,@cdptid,@invcode,@invname,@invspec,@number,@assistnum,@billbid)";
                    SqlCommand cmd = new SqlCommand(sql, sqlCon1);
                    SqlParameter parn = new SqlParameter("@billbid", dr["billbid"]);
                    cmd.Parameters.Add(parn);
                    SqlParameter parn1 = new SqlParameter("@pk_corp", dr["公司号"]);
                    cmd.Parameters.Add(parn1);
                    SqlParameter parn4 = new SqlParameter("@ncaccount", dr["帐套号"]);
                    cmd.Parameters.Add(parn4);
                    SqlParameter parn2 = new SqlParameter("@billtype", dr["单据类型"]);
                    cmd.Parameters.Add(parn2);
                    SqlParameter parn3 = new SqlParameter("@warehouse", dr["仓库"]);
                    cmd.Parameters.Add(parn3);
                    //SqlParameter parn4 =new SqlParameter ("@assistnum",dr["辅计量数量"]);
                    //cmd.Parameters.Add(parn4);


                    SqlParameter paro = new SqlParameter("@invcode", dr["存货编码"]);
                    cmd.Parameters.Add(paro);
                    SqlParameter parp = new SqlParameter("@invname", dr["存货名称"]);
                    cmd.Parameters.Add(parp);
                    SqlParameter para = new SqlParameter("@billdate", DbType.DateTime);
                    para.Value = DateTime.Parse(dr["单据日期"].ToString());
                    cmd.Parameters.Add(para);
                    SqlParameter parb = new SqlParameter("@billcode", dr["单据号"]);
                    cmd.Parameters.Add(parb);
                    SqlParameter parc = new SqlParameter("@cdptid", dr["部门"]);
                    cmd.Parameters.Add(parc);
                    SqlParameter pard = new SqlParameter("@invspec", dr["规格"]);
                    cmd.Parameters.Add(pard);
                    dr["数量"] = nullto0(Convert.ToString(dr["数量"]));
                    SqlParameter parm = new SqlParameter("@number", DbType.Decimal);
                    parm.Precision = 18;
                    parm.Scale = 4;
                    parm.Value = Decimal.Parse(dr["数量"].ToString()).ToString();
                    cmd.Parameters.Add(parm);
                    dr["辅计量数量"] = nullto0(Convert.ToString(dr["辅计量数量"]));
                    SqlParameter parh = new SqlParameter("@assistnum", DbType.Decimal);
                    parh.Value = Decimal.Parse(dr["辅计量数量"].ToString()).ToString();
                    parh.Precision = 18;
                    parh.Scale = 4;
                    cmd.Parameters.Add(parh);
                    //SqlParameter parx = new SqlParameter("@accountperiod", acpd1);
                    //cmd.Parameters.Add(parx);
                    i += cmd.ExecuteNonQuery();
                    cmd.Dispose();
                };
                Response.Write("<script>alert('同步成功,获取" + n + "条，同步" + i + "条');</script>");

            }
            catch { Response.Write("<script>alert('同步失败');</script>"); }
        }

        //获取NC材料出库,存入TABC_NC_StockOut
        protected void Button_StockOut_Click(object sender, EventArgs e)
        {
            //dataclear();
            //WebReference.SynBusinessItf cx = new WebReference.SynBusinessItf();
            string begindate = TextBox3.Text;
            string enddate = TextBox4.Text;
            String s = cx.getNC_StuffStockOut(TextBox10.Text, TextBox11.Text, begindate, enddate);    //材料出库

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(s);

            XmlNodeList topM = doc.DocumentElement.ChildNodes;
            DataTable dt = new DataTable();
            dt.Columns.Add("公司号");
            dt.Columns.Add("帐套号");
            dt.Columns.Add("单据类型");
            dt.Columns.Add("费用类型");
            dt.Columns.Add("仓库");
            dt.Columns.Add("单据日期");
            dt.Columns.Add("单据号");
            dt.Columns.Add("部门");
            //dt.Columns.Add("pk_corp");

            dt.Columns.Add("存货编码");
            dt.Columns.Add("存货名称");
            dt.Columns.Add("规格");
            dt.Columns.Add("型号");

            dt.Columns.Add("辅计量数量");
            dt.Columns.Add("数量");
            dt.Columns.Add("单价");
            dt.Columns.Add("金额");
            dt.Columns.Add("成本账户编码");
            dt.Columns.Add("作业中心编码");
            dt.Columns.Add("billbid");
            foreach (XmlElement element in topM)
            {
                if (element.Name.ToLower() == "bill")
                {
                    XmlNodeList nodelist = element.ChildNodes;
                    if (nodelist.Count > 0)
                    {
                        DataRow dr = dt.NewRow();
                        foreach (XmlElement el in nodelist)
                        {
                            XmlNodeList list = el.ChildNodes;
                            switch (el.Name)
                            {
                                case "bill_head":
                                    foreach (XmlElement li in list)
                                    {
                                        switch (li.Name)
                                        {
                                            //case "billid":
                                            //    dr["billid"] = li.InnerText;
                                            //    break;
                                            case "billtype":
                                                dr["单据类型"] = li.InnerText;
                                                break;
                                            case "pricetype":
                                                dr["费用类型"] = li.InnerText;
                                                break;
                                            case "warehouse":
                                                dr["仓库"] = li.InnerText;
                                                break;
                                            case "billcode":
                                                dr["单据号"] = li.InnerText;
                                                break;
                                            case "billdate":
                                                dr["单据日期"] = li.InnerText;
                                                break;
                                            case "cdptid":
                                                dr["部门"] = li.InnerText;
                                                break;
                                            case "pk_corp":
                                                dr["公司号"] = li.InnerText;
                                                break;
                                            case "ncaccount":
                                                dr["帐套号"] = li.InnerText;
                                                break;
                                        }
                                    }
                                    break;
                                case "body":
                                    foreach (XmlElement li in list)
                                    {
                                        DataRow dr1 = dt.NewRow();
                                        dr1["单据类型"] = dr["单据类型"];
                                        dr1["费用类型"] = dr["费用类型"];
                                        dr1["仓库"] = dr["仓库"];
                                        dr1["单据号"] = dr["单据号"];
                                        dr1["单据日期"] = dr["单据日期"];
                                        dr1["部门"] = dr["部门"];
                                        dr1["公司号"] = dr["公司号"];
                                        dr1["帐套号"] = dr["帐套号"];
                                        switch (li.Name)
                                        {
                                            case "entry":
                                                XmlNodeList list1 = li.ChildNodes;
                                                foreach (XmlElement li1 in list1)
                                                {
                                                    switch (li1.Name)
                                                    {
                                                        case "billbid":
                                                            dr1["billbid"] = li1.InnerText;
                                                            break;
                                                        case "invcode":
                                                            dr1["存货编码"] = li1.InnerText;
                                                            break;
                                                        case "invspec":
                                                            dr1["规格"] = li1.InnerText;
                                                            break;
                                                        case "invtype":
                                                            dr1["型号"] = li1.InnerText;
                                                            break;
                                                        case "invname":
                                                            dr1["存货名称"] = li1.InnerText;
                                                            break;
                                                        case "assistnum":
                                                            dr1["辅计量数量"] = li1.InnerText;
                                                            break;
                                                        case "number":
                                                            dr1["数量"] = li1.InnerText;
                                                            break;
                                                        case "nprice":
                                                            dr1["单价"] = li1.InnerText;
                                                            break;
                                                        case "nmny":
                                                            dr1["金额"] = li1.InnerText;
                                                            break;
                                                        case "costcode":
                                                            dr1["成本账户编码"] = li1.InnerText;
                                                            break;
                                                        case "wcentercode":
                                                            dr1["作业中心编码"] = li1.InnerText;
                                                            break;
                                                    }
                                                }
                                                dt.Rows.Add(dr1);

                                                break;
                                        }
                                    }
                                    break;
                            }
                        }
                        //dt.Rows.Add(dr);
                    }
                }
            };
            GridView1.DataSource = dt;
            GridView1.DataBind();
            int n = dt.Rows.Count;
            int i = 0;
            try
            {
                SqlConnection sqlCon1;
                //string strCon1 = "Data Source=localhost;Initial Catalog=hisun;Integrated Security=True";
                sqlCon1 = new SqlConnection(strCon1);
                sqlCon1.Open();

                string sql1 = @"delete from TABC_NC_StockOut";
                SqlCommand cmd1 = new SqlCommand(sql1, sqlCon1);
                cmd1.ExecuteNonQuery();
                cmd1.Dispose();
                foreach (DataRow dr in dt.Rows)
                {
                    //                string sql = @"if exists ( select * from TABC_JS_Product where 产品账户ID=@costcode and 来源='NC')
                    //                            update TABC_JS_Product set  产品账户名称=@costname,产品ID=@invcode,产品名称=@invname,规格型号=@invspec where 产品账户ID=@costcode and 来源='NC'                        
                    //                            else insert into TABC_JS_Product (产品账户ID,产品账户名称,产品ID,产品名称,规格型号,来源)
                    //                                        values(@costcode,@costname,@invcode,@invname,@invspec,'NC')";
                    string sql = @"if exists (select * from TABC_NC_StockOut where billbid=@billbid)
                            update TABC_NC_StockOut set 帐套号=@ncaccount,公司号=@pk_corp,单据类型=@billtype,费用类型=@pricetype,仓库=@warehouse,单据日期=@billdate,单据号=@billcode,部门=@cdptid,存货编码=@invcode,
                                存货名称=@invname,规格=@invspec,型号=@invtype,辅计量数量=@assistnum,成本账户编码=@costcode,数量=@number,金额=@nmny,单价=@nprice,作业中心编码=@wcentercode
                                where billbid=@billbid
                            else insert into TABC_NC_StockOut 
                            values(@pk_corp,@ncaccount,@billtype,@pricetype,@warehouse,@billdate,@billcode,@cdptid,@invcode,@invname,@invspec,@invtype,@assistnum,@number,@nmny,@nprice,@costcode,@wcentercode,@billbid)";
                    SqlCommand cmd = new SqlCommand(sql, sqlCon1);
                    SqlParameter parc = new SqlParameter("@pk_corp", dr["公司号"]);
                    cmd.Parameters.Add(parc);
                    SqlParameter par1 = new SqlParameter("@ncaccount", dr["帐套号"]);
                    cmd.Parameters.Add(par1);
                    SqlParameter parb = new SqlParameter("@billtype", dr["单据类型"]);
                    cmd.Parameters.Add(parb);
                    SqlParameter par3 = new SqlParameter("@pricetype", dr["费用类型"]);
                    cmd.Parameters.Add(par3);
                    SqlParameter pard = new SqlParameter("@warehouse", dr["仓库"]);
                    cmd.Parameters.Add(pard);
                    SqlParameter pare = new SqlParameter("@billcode", dr["单据号"]);
                    cmd.Parameters.Add(pare);
                    SqlParameter parn = new SqlParameter("@cdptid", dr["部门"]);
                    cmd.Parameters.Add(parn);
                    SqlParameter park = new SqlParameter("@wcentercode", dr["作业中心编码"]);
                    cmd.Parameters.Add(park);
                    SqlParameter parr = new SqlParameter("@costcode", dr["成本账户编码"]);
                    cmd.Parameters.Add(parr);
                    SqlParameter para = new SqlParameter("@billdate", DbType.DateTime);
                    para.Value = DateTime.Parse(dr["单据日期"].ToString());
                    cmd.Parameters.Add(para);
                    SqlParameter paro = new SqlParameter("@invcode", dr["存货编码"]);
                    cmd.Parameters.Add(paro);
                    SqlParameter parp = new SqlParameter("@invname", dr["存货名称"]);
                    cmd.Parameters.Add(parp);
                    SqlParameter parq = new SqlParameter("@invspec", dr["规格"]);
                    cmd.Parameters.Add(parq);
                    SqlParameter parj = new SqlParameter("@invtype", dr["型号"]);
                    cmd.Parameters.Add(parj);
                    dr["数量"] = nullto0(Convert.ToString(dr["数量"]));
                    SqlParameter parm = new SqlParameter("@number", DbType.Decimal);
                    parm.Precision = 18;
                    parm.Scale = 4;
                    parm.Value = Decimal.Parse(dr["数量"].ToString()).ToString();
                    cmd.Parameters.Add(parm);
                    //decimal b = decimal.Parse(dr["number"].ToString());
                    //SqlParameter parq = new SqlParameter("@number", dr["number"]);
                    //cmd.Parameters.Add(parq);
                    dr["金额"] = nullto0(Convert.ToString(dr["金额"]));
                    SqlParameter parf = new SqlParameter("@nmny", DbType.Decimal);
                    parf.Scale = 4;
                    parf.Precision = 18;
                    parf.Value = Decimal.Parse(dr["金额"].ToString()).ToString();
                    cmd.Parameters.Add(parf);
                    dr["单价"] = nullto0(Convert.ToString(dr["单价"]));
                    SqlParameter parg = new SqlParameter("@nprice", DbType.Decimal);
                    parg.Value = Decimal.Parse(dr["单价"].ToString()).ToString();
                    parg.Precision = 18;
                    parg.Scale = 8;
                    cmd.Parameters.Add(parg);
                    dr["辅计量数量"] = nullto0(Convert.ToString(dr["辅计量数量"]));
                    SqlParameter parh = new SqlParameter("@assistnum", DbType.Decimal);
                    parh.Value = Decimal.Parse(dr["辅计量数量"].ToString()).ToString();
                    parh.Precision = 18;
                    parh.Scale = 4;
                    cmd.Parameters.Add(parh);
                    SqlParameter pari = new SqlParameter("@billbid", dr["billbid"]);
                    cmd.Parameters.Add(pari);

                    i += cmd.ExecuteNonQuery();
                    cmd.Dispose();
                };
                Response.Write("<script>alert('同步成功,获取" + n + "条，同步" + i + "条');</script>");

            }
            catch { Response.Write("<script>alert('同步失败');</script>"); }
        }

        //获取NC总账凭证 ,新建TABC_NC_Voucher
        protected void Button_Voucher_Click(object sender, EventArgs e)
        {
            //dataclear();
            // WebReference.SynBusinessItf cx = new WebReference.SynBusinessItf();
            string begindate = TextBox5.Text;
            string enddate = TextBox6.Text;
            String s = cx.getNC_Voucher(TextBox10.Text, TextBox11.Text, begindate, enddate);     //获取NC总账凭证

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(s);
            DataTable dt = new DataTable();
            dt.Columns.Add("凭证主键");
            dt.Columns.Add("凭证号");
            dt.Columns.Add("凭证期间");
            dt.Columns.Add("公司号");
            dt.Columns.Add("帐套号");
            dt.Columns.Add("部门编码");
            dt.Columns.Add("部门名称");
            dt.Columns.Add("作业中心编码");
            dt.Columns.Add("作业中心名称");
            dt.Columns.Add("成本账户编码");//成本账户编码
            dt.Columns.Add("成本账户名称");//成本账户名称
            dt.Columns.Add("科目");
            dt.Columns.Add("制单日期");
            dt.Columns.Add("本币贷发生额");
            dt.Columns.Add("本币借发生额");
            dt.Columns.Add("辅助核算编码");
            dt.Columns.Add("辅助核算名称");
            XmlNodeList topM = doc.DocumentElement.ChildNodes;
            foreach (XmlElement element in topM)
            {
                XmlNodeList nodelist = element.ChildNodes;
                if (nodelist.Count > 0)
                {
                    DataRow dr = dt.NewRow();
                    foreach (XmlElement el in nodelist)
                    {
                        switch (el.Name)
                        {
                            case "pk_voucher":
                                dr["凭证主键"] = el.InnerText;
                                break;
                            case "pk_corp":
                                dr["公司号"] = el.InnerText;
                                break;
                            case "ncaccount":
                                dr["帐套号"] = el.InnerText;
                                break;
                            case "costname":
                                dr["成本账户名称"] = el.InnerText;
                                break;
                            case "costcode":
                                dr["成本账户编码"] = el.InnerText;
                                break;
                            case "wcentercode":
                                dr["作业中心编码"] = el.InnerText;
                                break;
                            case "wcentername":
                                dr["作业中心名称"] = el.InnerText;
                                break;
                            case "accsubj":
                                dr["科目"] = el.InnerText;
                                break;
                            case "checkdate":
                                dr["制单日期"] = el.InnerText;
                                break;
                            case "localcreditamount":
                                dr["本币贷发生额"] = el.InnerText;
                                break;
                            case "localdebitamount":
                                dr["本币借发生额"] = el.InnerText;
                                break;
                            case "valuecode":
                                dr["辅助核算编码"] = el.InnerText;
                                break;
                            case "valuename":
                                dr["辅助核算名称"] = el.InnerText;
                                break;
                            case "no":
                                dr["凭证号"] = el.InnerText;
                                break;
                            case "period":
                                dr["凭证期间"] = el.InnerText;
                                break;
                            case "deptcode":
                                dr["部门编码"] = el.InnerText;
                                break;
                            case "deptname":
                                dr["部门名称"] = el.InnerText;
                                break;
                        }
                    }
                    dt.Rows.Add(dr);
                }
            };
            GridView1.DataSource = dt;
            GridView1.DataBind();
            int n = dt.Rows.Count;
            int i = 0;
            try
            {
                SqlConnection sqlCon1;
                //string strCon1 = "Data Source=localhost;Initial Catalog=hisun;Integrated Security=True";
                sqlCon1 = new SqlConnection(strCon1);
                sqlCon1.Open();

                string sql1 = @"delete from TABC_NC_Voucher";
                SqlCommand cmd1 = new SqlCommand(sql1, sqlCon1);
                cmd1.ExecuteNonQuery();
                cmd1.Dispose();
                foreach (DataRow dr in dt.Rows)
                {

                    string sql = @"if exists (select * from TABC_NC_Voucher where 凭证主键=@凭证主键)
                            update TABC_NC_Voucher set  凭证号=@凭证号,凭证期间=@凭证期间,部门编码=@部门编码,部门名称=@部门名称,公司号=@公司号,帐套号=@ncaccount,作业中心编码=@作业中心编码,作业中心名称=@作业中心名称,成本账户编码=@成本账户编码,成本账户名称=@成本账户名称,
                            科目=@科目,制单日期=@制单日期,本币贷发生额=@本币贷发生额,本币借发生额=@本币借发生额,辅助核算编码=@辅助核算编码,辅助核算名称=@辅助核算名称 where 凭证主键=@凭证主键
                            else insert into TABC_NC_Voucher
                             values(@凭证主键,@凭证号,@凭证期间,@公司号,@ncaccount,@部门编码,@部门名称,@作业中心编码,@作业中心名称,@成本账户编码,@成本账户名称,@科目,@制单日期,@本币贷发生额,@本币借发生额,@辅助核算编码,@辅助核算名称)";
                    //                string sql=@"insert into TABC_NC_Voucher
                    //                             values(@凭证主键,@作业中心编码,@作业中心名称,@成本账户编码,@成本账户名称,@科目,@制单日期,@本币贷发生额,@本币借发生额,@辅助核算编码,@辅助核算名称)";
                    SqlCommand cmd = new SqlCommand(sql, sqlCon1);
                    SqlParameter parn = new SqlParameter("@凭证主键", dr["凭证主键"]);
                    cmd.Parameters.Add(parn);
                    SqlParameter pars = new SqlParameter("@公司号", dr["公司号"]);
                    cmd.Parameters.Add(pars);
                    SqlParameter par1 = new SqlParameter("@ncaccount", dr["帐套号"]);
                    cmd.Parameters.Add(par1);
                    SqlParameter parr = new SqlParameter("@作业中心编码", dr["作业中心编码"]);
                    cmd.Parameters.Add(parr);
                    SqlParameter para = new SqlParameter("@作业中心名称", dr["作业中心名称"]);
                    cmd.Parameters.Add(para);
                    SqlParameter parb = new SqlParameter("@成本账户编码", dr["成本账户编码"]);
                    cmd.Parameters.Add(parb);
                    SqlParameter parc = new SqlParameter("@成本账户名称", dr["成本账户名称"]);
                    cmd.Parameters.Add(parc);
                    SqlParameter pard = new SqlParameter("@科目", dr["科目"]);
                    cmd.Parameters.Add(pard);
                    SqlParameter pare = new SqlParameter("@制单日期", DbType.DateTime);
                    pare.Value = DateTime.Parse(dr["制单日期"].ToString());
                    cmd.Parameters.Add(pare);

                    SqlParameter parm = new SqlParameter("@本币贷发生额", DbType.Decimal);
                    parm.Precision = 18;
                    parm.Scale = 4;
                    parm.Value = Decimal.Parse(dr["本币贷发生额"].ToString()).ToString();
                    cmd.Parameters.Add(parm);
                    SqlParameter paro = new SqlParameter("@本币借发生额", DbType.Decimal);
                    paro.Scale = 4;
                    paro.Precision = 18;
                    // parb.Value = dr["本币借发生额"].ToString();
                    paro.Value = Decimal.Parse(dr["本币借发生额"].ToString()).ToString();
                    cmd.Parameters.Add(paro);
                    SqlParameter parx = new SqlParameter("@辅助核算编码", dr["辅助核算编码"]);
                    cmd.Parameters.Add(parx);
                    SqlParameter parw = new SqlParameter("@辅助核算名称", dr["辅助核算名称"]);
                    cmd.Parameters.Add(parw);
                    SqlParameter pzh = new SqlParameter("@凭证号", dr["凭证号"]);
                    cmd.Parameters.Add(pzh);
                    SqlParameter pzqj = new SqlParameter("@凭证期间", dr["凭证期间"]);
                    cmd.Parameters.Add(pzqj);
                    SqlParameter dc = new SqlParameter("@部门编码", dr["部门编码"]);
                    cmd.Parameters.Add(dc);
                    SqlParameter dn = new SqlParameter("@部门名称", dr["部门名称"]);
                    cmd.Parameters.Add(dn);

                    i += cmd.ExecuteNonQuery();
                    cmd.Dispose();
                };
                Response.Write("<script>alert('同步成功,获取" + n + "条，同步" + i + "条');</script>");

            }
            catch { Response.Write("<script>alert('同步失败');</script>"); }
        }


        protected string nullto0(string s)
        {
            if (s == "null")
            {
                s = "0";
            }
            return s;
        }

        protected void Button8_Click(object sender, EventArgs e)
        {
            SqlConnection sqlCon1;
            sqlCon1 = new SqlConnection(strCon1);
            sqlCon1.Open();
            string sql = @"select * from TABC_NC_StockIn ";
            SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, sqlCon1);
            DataSet ds = new DataSet();
            dataAdapter.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }

        protected void Button9_Click(object sender, EventArgs e)
        {
            SqlConnection sqlCon1;
            sqlCon1 = new SqlConnection(strCon1);
            sqlCon1.Open();
            string sql = @"select * from TABC_NC_StockOut ";
            SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, sqlCon1);
            DataSet ds = new DataSet();
            dataAdapter.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }

        protected void Button10_Click(object sender, EventArgs e)
        {
            SqlConnection sqlCon1;
            sqlCon1 = new SqlConnection(strCon1);
            sqlCon1.Open();
            string sql = @"select * from TABC_NC_Voucher ";
            SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, sqlCon1);
            DataSet ds = new DataSet();
            dataAdapter.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }

        protected void Button_Edit_Click(object sender, EventArgs e)
        {
            TextBox10.ReadOnly = false;
            TextBox9.ReadOnly = false;
            TextBox11.ReadOnly = false;

        }

        protected void Button7_Click(object sender, EventArgs e)
        {
            if (TextBox9.Text == "" || TextBox10.Text == "" || TextBox11.Text == "")
            { Response.Write("<script>alert('输入有误,请重新输入');</script>"); }
            else
            {
                Session["aid"] = TextBox10.Text;
                Session["cid"] = TextBox11.Text;
                Session["ip"] = TextBox9.Text;

                Response.Write("<script>alert('修改成功');</script>");
                TextBox10.ReadOnly = true;
                TextBox9.ReadOnly = true;
                TextBox11.ReadOnly = true;

            }

        }
    }
}
